
export const util = {
	setUser: function(userInfo) {
		try {
			//todo del
			uni.setStorageSync("userInfo", userInfo);
		} catch (e) {
			// error
		}
	},
	getUser: function() {
		try {
			const value = uni.getStorageSync("userInfo");
			//todo del
			if (value) {
				return value;
			}
			return {}
		} catch (e) {
			// error
		}
	},
	
	setChild: function(childInfo) {
		try {
			//todo del
			uni.setStorageSync("childInfo", childInfo);
		} catch (e) {
			// error
		}
	},
	getChild: function() {
		try {
			const value = uni.getStorageSync("childInfo");
			//todo del
			if (value) {
				return value;
			}
			return {}
		} catch (e) {
			// error
		}
	},
	
	
	//引导页面
	setGuide: function(launchFlag) {
		try {
			uni.setStorageSync("launchFlag", launchFlag);
		} catch (e) {
			// error
		}
	},
	getGuide: function() {
		try {
			const value = uni.getStorageSync("launchFlag");
			if (value) {
				return value;
			}
			return {}
		} catch (e) {
			// error
		}
	},
	 callQueryResult: function(callback,arg){
		setTimeout(()=>{
			uni.hideLoading();
			callback(arg);
		},500)
	},
	
	//清空缓存
	clearCache: function() {
		try {
			uni.clearStorageSync();
		} catch (e) {
			// error
		}
	},

	//生产uuid
	getUUID: function() {
		return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
			return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
		})
	},

	//时间戳转换对象
	timestampToTime: function(timestamp) {
		var strDate; 
		var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
		var Y = date.getFullYear() + '-';
		var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
		var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
		var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
		var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
		var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());

		strDate = Y + M + D + h + m + s;
		return strDate;
	},
	// 请销假式时间计算
	getDays(startDate,endDate){
		var date1Str = startDate.split("-");//将日期字符串分隔为数组，数组元素分别为年、月、日      
		//根据年、月、日的值创建Date对象
		var date1Obj = new Date(date1Str[0],(date1Str[1]-1),date1Str[2]);
		var date2Str = endDate.split("-");
		var date2Obj = new Date(date2Str[0],(date2Str[1]-1),date2Str[2]);
		var t1 = date1Obj.getTime();//返回从1970-1-1开始计算到Date对象中的时间之间的毫秒数
		var t2 = date2Obj.getTime();//返回从1970-1-1开始计算到Date对象中的时间之间的毫秒数
		var datetime=1000*60*60*24; //一天时间的毫秒值 
		var minusDays = Math.floor(((t2-t1)/datetime));//计算出两个日期天数差 
		var days = Math.abs(minusDays);//如果结果为负数，取绝对值
		minusDays = minusDays+1;
		return minusDays;
	},
	// 计算天数
	GetDateDiff:function (startDate,endDate)
	{
	var startTime = new Date(startDate.replace(/-/g, "/")).getTime();
	var endTime = new Date(endDate.replace(/-/g, "/")).getTime();
	var dates = Math.floor((endTime-startTime))/(1000*60*60*24);
	return dates.toFixed(1);
	},
	// 计算不满一天转化小时和分钟
	GetDateDiffhours:function (startDate,endDate)
	 {
	 var startTime = new Date(startDate.replace(/-/g, "/")).getTime();
	 var endTime = new Date(endDate.replace(/-/g, "/")).getTime();
	 var dates = Math.floor(endTime-startTime);
	 var newDates = '';
	 if( dates < 36e5 ){
	  return newDates = Math.round(dates/6e4) +'分钟';
	 }
	 if(dates <  864e5 ){
		var hm  = dates/(1000*60*60);
		var h=parseInt(hm);
		var m=parseInt((hm-h)*60)
		if(m!==0){
			return newDates = ( h+"小时"+m+"分钟" );
		}else{
			return newDates = ( h+"小时" );
		}
	 }
	 if( dates < 2592e6 ){
	  return newDates = Math.round(dates/864e5) +'天';
	 }
	},
	
	// 加班计算时间全按小时来算
	
	overtimeHours:function (startDate,endDate)
	 {
	 if(isNaN(startDate && endDate)){ 
		var startTime = new Date(startDate.replace(/-/g, "/")).getTime();
		var endTime = new Date(endDate.replace(/-/g, "/")).getTime();
		var dates = Math.floor(endTime-startTime);
		var newDates = '';
		var hm  = dates/(1000*60*60);
		var h=parseInt(hm);
		var m=parseInt((hm-h)*60)
		return newDates = h ;
	 }
	
	},
	
	timestampToTimeNew: function(timestamp) {
		var strDate; 
		var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
		var Y = date.getFullYear() + '-';
		var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
		var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
		strDate = Y + M + D;
		return strDate;
	},
	
	//比较二个时间大小(格式2017-01-11)
	compareDate: function(d1,d2)
	{
	  return ((new Date(d1.replace(/-/g,"\/"))) > (new Date(d2.replace(/-/g,"\/"))));
	},
	
	comparenewDate: function(d1,d2)
	{
	  return ((new Date(d1.replace(/-/g,"\/"))) >= (new Date(d2.replace(/-/g,"\/"))));
	},
	/**
	 *  数组对象去重
	 *  @param {array} arr 
	 *  @param {string} 去重字段 
	 * 	@return {array} newarray
	 * */
	repeat: function(arr, para) {
		var newArr = [];
		for (var i = 0; i < arr.length; i++) {
			if (newArr.indexOf(arr[i].para) == -1) {
				newArr.push(arr[i]);
			}
		}
		return newArr;
	},
	
	/**
	 *  数组深拷贝
	 * 	@param {array} arr 
	 * 	@return {array} newArray
	 * */
	copyArr: function(arr) {
	    let res = []
	    for (let i = 0; i < arr.length; i++) {
	     res.push(arr[i])
	    }
	    return res
	},
	
	/**
	 *  百分数转小数
	 * 	@param {string} percent
	 * 	
	 * */
	percentToPoint: function(percent){
	    var str = percent.replace("%", "");
	    str = str / 100;
	    return str;
	},
	/**
	 * 时间处理
	 */
	getTimer(stringTime){
		var minute = 1000 * 60;
		var hour = minute *60;
		var day = hour *24;
	    var time1 = new Date().getTime();//当前的时间戳
	    var time2 = Date.parse(new Date(stringTime));//指定时间的时间戳
	    var time = time1 - time2;
	    var result = null;
		if (time/day >= 30) {
		    result =stringTime.slice(0,10);
		}else if(time/day >= 1){
			result = parseInt(time/day) + "天前";
	    }else if(time/hour >= 1){
	        result = parseInt(time/hour) + "小时前";
	    }else if(time/minute >= 1){
	        result = parseInt(time/minute) + "分钟前";
	    }else {
	        result = "刚刚";
	    }
		return result
	},
	
	
	setdictionary: function(obj) {
		try {
			//todo del
			uni.setStorageSync("getdictionary", obj);
		} catch (e) {
			// error
		}
	},
	getdictionary: function(name) {
		try {
			const value = uni.getStorageSync("getdictionary");
			if (value) {
				let list = [];
				list = value.filter((k) => {
					if(k.name == name){
						return k;
					}
				});
				return list[0].sysDictDOList[0];
			}
			// return {}
		} catch (e) {
			// error
		}
	},
	
	
	setDropdownoptions: function(data) {
		try {
			//todo del
			uni.setStorageSync("Dropdownoptionslist", data);
		} catch (e) {
			// error
		}
	},
	
	
	getDropdownoptions: function() {
		try {
			const value = uni.getStorageSync("Dropdownoptionslist");
			//todo del
			if (value) {
				return value;
			}
			return {}
		} catch (e) {
			// error
		}
	},
	
	setnewMenudata: function(data) {
		try {
			//todo del
			uni.setStorageSync("newMenudata", data);
		} catch (e) {
			// error
		}
	},
	
	getnewMenudata: function() {
		try {
			const value = uni.getStorageSync("newMenudata");
			//todo del
			if (value) {
				return value;
			}
			return {}
		} catch (e) {
			// error
		}
	},
	
	
	
	//格式化时间
	format(dat){
	    //获取年月日，时间
	    var year = dat.getFullYear();
	    var mon = (dat.getMonth()+1) < 10 ? "0"+(dat.getMonth()+1) : dat.getMonth()+1;
	    var data = dat.getDate()  < 10 ? "0"+(dat.getDate()) : dat.getDate();
	    var hour = dat.getHours()  < 10 ? "0"+(dat.getHours()) : dat.getHours();
	    var min =  dat.getMinutes()  < 10 ? "0"+(dat.getMinutes()) : dat.getMinutes();
	    var seon = dat.getSeconds() < 10 ? "0"+(dat.getSeconds()) : dat.getSeconds();
	                 
	    var newDate = year +"-"+ mon +"-"+ data +" "+ hour +":"+ min +":"+ seon;
	    return newDate;
	}
	
}
